package com.qqtop10.manager.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qqtop10.manager.service.SellerRankingService;

/**
 * 首页控制器
 * 
 * @author Jovi
 *
 */
@RequestMapping("/manager/")
@Controller
public class IndexController extends BaseController {

	@Autowired
	private SellerRankingService sellerRankingService;

	/**
	 * 商家销售排行
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("sellerRanking.do")
	public void orderRanking(HttpServletRequest request,
			HttpServletResponse response, String type) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/javascript; charset=utf-8");
		Map<String, Object> map = new HashMap<>();
		StringBuffer where = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 总排行或者是今日排行
		if (type != null && type.equals("1")) {
			where = new StringBuffer();
			where.append(" WHERE DATE_FORMAT(tbo.createtime, '%Y-%m-%d') = '"
					+ sdf.format(new Date()) + "'");
		}
		map.put("where", where);
		map.put("group", " GROUP BY tbo.shop_uuid");
		map.put("column",
				"tbs.`name`,CONVERT(SUM(tbo.actual_price+tbo.walletpay),DECIMAL(10,2)) sales,COUNT(tbo.uuid) count");
		map.put("from",
				" FROM tb_order tbo INNER JOIN tb_shop tbs ON tbo.shop_uuid = tbs.uuid");
		map.put("order", " ORDER BY sales DESC");
		response.getWriter().print(super.page(map, sellerRankingService, request));
	}
}
